#include "Arduino.h"
void setup();
void loop();
// 
#include <JeeLib.h>
#include <util/crc16.h>
#include <util/parity.h>
#include <avr/eeprom.h>
#include <avr/pgmspace.h>
#include <mesh_cfg.h>
//


// Define system globals
MilliTimer sendTimer;
byte needToSend;
byte srcAddr;
static unsigned long now() {
	//return relative time since startup
	// Note: current logic only supports 49-Day timer resolution
	//	before an overflow condition occurs 
	// TODO: FIXME
	return millis() / 1000;
}


typedef struct {
	byte  	destAddr;
	byte	msgType;
	byte	portA;
	byte	portB;
	byte	portC;
	byte	portD; 
} Msg;

// define the standard message payload struct
// Each message type the collect
static Msg msg;

void setup() {
	// Set serial interface speed from mesh1_cfg.h
	Serial.begin(TTY_PORT_SPEED);
	// Print startup message
	Serial.println("Starting up... ");
	Serial.print("NodeID:  ");
	Serial.println(NODE_ID);
	// initialize radio with preconfigured network values. 
   	 // values stored in mesh1_cfg.h
	rf12_initialize(NODE_ID, RF12_BAND, GROUP_ID);
}
/*
bool msgCheckNode () {
	// Read message payload determine 
}
*/

void loop() {
	// Was a message received, is it for me?
	if (rf12_recvDone() && rf12_crc == 0  ) {
	//if (rf12_recvDone() && rf12_crc == 0  ) {
		// valid message received, send context to msgProcessor.
		srcAddr = RF12_HDR_MASK & rf12_hdr;		


		Serial.print("Msg Header: ");	
		Serial.println(rf12_hdr, BIN);	
		// is the message for me?
			
		//Serial.print("Msg Header Address: ");	
		//Serial.println(srcAddr);	
			

			
		if (srcAddr == NODE_ID ) {
			Serial.println("Message is for ME!!! Reading payload...");
		} else   {
			Serial.print("Broadcast Message Received, Reading Packet from : ");
			Serial.println(srcAddr);	
		} 
		


		// Cast a valid message into a message structure...
		// Contiune processing...		
		//const Msg* msg_r = (const Msg*) rf12_data;			


		//Serial.print("[ ");
		//Serial.print(msg_r->destAddr);
		//Serial.print(" ] Received message from NodeId: ");
		/*
		for (byte i = 0; i < rf12_len; i++ ) {
			Serial.print(rf12_data[1]);
		}
		*/
		// Get the reply to address from the header 
		//Serial.print( RF12_HDR_MASK & rf12_hdr);
		//Serial.print(" --");
		//Serial.println();
		// add pause for settling
		delay(10);
				
	}

	if (sendTimer.poll(2000) && NODE_ID != 3) {
		// Check timer to determine if its time to send msg
		needToSend = 1;
		
	}


	if (needToSend && rf12_canSend() ) {
		needToSend = 0;
		// send message to specific node
		msg.destAddr = 3; // hard coded for testing. 
		Serial.println("Sending to Node 3 w/DST");			
		rf12_sendStart(msg.destAddr | RF12_HDR_DST, &msg, sizeof msg);
		delay(500);
		
		msg.destAddr = 0; // hard coded for testing. 
		Serial.print("Sending msg to: " );
		Serial.println(msg.destAddr);
			
		rf12_sendStart(msg.destAddr , &msg, sizeof msg);
		delay(500);
		// send message to specific node
		//msg.destAddr = 25; // hard coded for testing. 
		//msg.payLoad = "Hello Number 25"; // hard coded for testing. 
		//Serial.println("Sending to Node 25");			
		//rf12_sendStart(msg.destAddr, &msg, sizeof msg);
		//delay(10);
		//strcpy(msg.payLoad,  payload);
		

		//Serial.print("Struct nodeId-> ");
		//Serial.println(msg.srcAddr);
		//Serial.print("Struct Message -> ");
		//Serial.println(msg.payLoad);
		//Serial.println("Sending to Node 3");			
		//rf12_sendStart(3, payload, sizeof payload);
		//delay(10);
		//Serial.println("Sending to Node 25");			
		//rf12_sendStart(msg., payload, sizeof payload);
		//delay(10);
		//Serial.println("Sending to Broadcast.........");			
		//rf12_sendStart(0, payload, sizeof payload);
		//delay(340);
		//rf12_sendStart(0, &msg, sizeof msg);
	}	

	/*
	while (!rf12_canSend())
			// receive messages while transmission channel is busy.
    		rf12_recvDone();
  	// send an empty packet to one specific pollee
  	rf12_sendStart(RF12_HDR_ACK | RF12_HDR_DST | nextId, 0, 0);
  	// wait up to 10 milliseconds for a reply
  	timer.set(10);
  	while (!timer.poll())
        if (rf12_recvDone() && rf12_crc == 0 && rf12_len == sizeof (Payload)) {
      		// got a good ACK packet, print out its contents
            const Payload* p = (const Payload*) rf12_data;
            Serial.print((word) p->node);
            Serial.print(": ");
            Serial.println(p->time);
            break;
        }

	*/


}
